# -*- coding: utf-8 -*-
# @Time    : 2022/5/24 22:25
# @Author  : leijiu
# @Email   : 517711635@qq.com
# @File    : list_goods_page.py
# @Software: PyCharm
"""
商品列表页面
"""
from selenium.webdriver.common.by import By
from time import sleep
from page.base_page import BasePage

class GoodsList(BasePage):
    # def __init__(self, driver):
    #     self.driver = driver
    #添加商品定位器
    ele_goodsadmin_locator = (By.CSS_SELECTOR, "#menu-ul > li.collapse.lis.ico_1")
    ele_goodslist_locator = (By.LINK_TEXT, "商品列表")
    ele_addgoods_locator = (By.LINK_TEXT, "添加新商品")
    #编辑定位器

    ele_goods_edit_locator = (By.XPATH,"//*[@id='listDiv']/table[1]/tbody/tr[3]/td[11]/a[2]/img")
    #删除定位器
    ele_tbody_loctor = (By.CSS_SELECTOR, "#listDiv > table:nth-child(1)")
    ele_tr_locator = (By.TAG_NAME, "tr")
    ele_td_locator = (By.TAG_NAME, "td")
    ele_a_loctor = (By.TAG_NAME, "a")
    #搜索定位器
    ele_search_keyword_locator = (By.NAME, "keyword")
    ele_search_keyword_btn_locator = (By.CSS_SELECTOR, "body > div.form-div > form > input.button")

    #切换到左边页面
    def leftpage(self):
        self.driver.switch_to.frame("menu-frame")
        sleep(1)
        # 点击商品管理
    def goodsadmin(self):
        self.driver.find_element(*self.ele_goodsadmin_locator).click()
        sleep(1)
        # 点击商品列表
    def goodslist(self):
        self.driver.find_element(*self.ele_goodslist_locator).click()
        sleep(1)
        # 切换到主页面
    def mainpage(self):
        self.driver.switch_to.parent_frame()
        self.driver.switch_to.frame("main-frame")
        sleep(1)
        #点击添加商品
    def addgoods(self):
        self.driver.find_element(*self.ele_addgoods_locator).click()
        # 搜索商品
    def searchgoods(self):
        self.driver.find_element(*self.ele_search_keyword_locator).send_keys("天子")
        self.driver.find_element(*self.ele_search_keyword_btn_locator).click()
        #点击编辑
    def altergoods(self):
        self.driver.find_element(*self.ele_goods_edit_locator).click()
        # 删除商品
    def deletegoods(self):
        tbody_element=self.driver.find_element(*self.ele_tbody_loctor)
        trs=self.driver.find_elements(*self.ele_tr_locator)[2:]
        for tr in trs:
            tds = tr.find_elements(*self.ele_td_locator)
            second_td = tds[1]
            if second_td.text == "天子":
                last_td = tds[-1]
                delete_element = last_td.find_elements(*self.ele_a_loctor)[-1]
                delete_element.click()
                sleep(2)
                self.driver.switch_to.alert.accept()
                break
    def ad(self):
        return self.driver.find_element(By.CSS_SELECTOR,"#totalRecords").text
